﻿using System;
using UnityEngine;

namespace PaperMaster.Utilities
{
    /// <summary>
    /// 在使用该类及其子类封装的数据时，可以同时指定一个其值被更新时的操作。
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class BindableProperty<T>
    {
        protected T _value;
        protected Action _onSet;
        public virtual T v
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
                _onSet?.Invoke();
            }
        }
        public BindableProperty(Action onSet=null){
            _onSet = onSet;
        }
        public void Register(Action onSet)
        {
            _onSet += onSet;
        }
        /// <summary>
        /// 手动激活
        /// </summary>
        public void Invoke()
        {
            _onSet?.Invoke();
        }
        ~BindableProperty()
        {
            _onSet = null;
        }
    }
}
